-------------------------------------------------------------------------------
--
-------------------------------------------------------------------------------
require("luarocks.require")
require("luasql.mysql")
require("recaptcha")
local smtp = require("socket.smtp")
local io = require("io")

dofile("common.lua")

local smtpConfFileName = "./conf/serverSMTP.conf"
local mysqlConfFileName = "./conf/serverMYSQL.conf"
local recaptchaConfFileName = "./conf/recaptcha.conf"
local webConfFileName = "./conf/serverWEB.conf"
local msgBodyFileName = "./msgBodyConfirmation.txt"

-------------------------------------------------------------------------------
-- Function Check entered password
-------------------------------------------------------------------------------
function checkPassword()
	if (cgilua.POST.pass == cgilua.POST.repass) then
		return true
	else
		cgilua.put([[
		Attention!!! Password incorrect, please try again...
		]])
		return false
	end
	 
end

-------------------------------------------------------------------------------
-- Function of send mail of confirmation
-------------------------------------------------------------------------------
function sentMessageOfConfimation(userID, email, password, IPMP)
	local from = "<"..getParameter("SMTP_USER")..">"
	local rcpt = "<"..tostring(email)..">"

	local msgRandomIDStr = "This is the acknowledgement of your registration in the DistriNet network.\n\n"
	local msgRandomIDStr = msgRandomIDStr.."Your ID is :  "..tostring(IPMP).."."..userID.."\n\n"
	
	msgRandomIDStr = msgRandomIDStr.."You will need this ID to login next time to the network.\n\n"
	msgRandomIDStr = msgRandomIDStr.."Please click on the following link to confirm your registration:".."\n\n"
	msgRandomIDStr = msgRandomIDStr.."http://"..getDomainNameServerWeb()..":"..getPortServerWeb().."/confirmation.lp?IPMP="..tostring(IPMP).."&userID="..tostring(userID).."&password="..tostring(password).."&type=registration"
	
	local msgBodyFile = assert(io.open(msgBodyFileName,"r"))
	local msgBodyStr = msgBodyFile:read("*a")
	msgBodyFile:close()
	
	local msg = {
		headers = {
			from = "<"..getParameter("SMTP_USER")..">",
			to = "<"..tostring(email)..">",
			subject = "Confirmation of Distrinet",
		},
		body = 	msgRandomIDStr.."\n"..msgBodyStr
	}
	
	local packet = {}
	packet.from = from
	packet.rcpt = rcpt
	packet.source = smtp.message(msg)
	packet.server = getParameter("SMTP_SERVER_NAME")
	packet.port = getParameter("SMTP_SERVER_PORT")
	packet.user = getParameter("SMTP_USER")
	--packet.password = getParameter("SMTP_PASSWORD")
	
	-- Send Email via SMTP
	local ok_send, sendErrStr = smtp.send(packet)
	if ok_send then
		print("C'est parfait !!! Envoyer")
	end
	print(ok_send, sendErrStr)
end

-------------------------------------------------------------------------------
-- Check exist email in the database
-------------------------------------------------------------------------------
function checkExistEmail (email)
	local executionStr = string.format([[SELECT * FROM master_peer WHERE email = '%s']], tostring(email))
	local execution = db_Execute (executionStr)
	local row = execution:fetch ({}, "a")
	if (row) then
		return false -- exist in the database
	else
		return true -- not exist in the database
	end
end

-------------------------------------------------------------------------------
-- Register a new user in the database with status = NON_CONFIRMATION
-------------------------------------------------------------------------------
function registerUserIntoDatabaseOfMasterPeer(userID, password, email) 
	local executionStr = string.format([[INSERT INTO master_peer (userID, email, password, status) VALUES('%d', '%s', '%s', '%s')]], tonumber(userID), tostring(email), tostring(password), "NON_CONFIRMATION")
	local execution = db_Execute (executionStr)
end


--function getRecaptcha()
--	local recaptchaParameters = getRecaptchaParametersFromConfFile()
--	captcha = recaptcha.new{recaptchaParameters.publicKey, recaptchaParameters.privateKey }
--	cgilua.put("<%captcha:get_html()%>")
--end








